﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace SlideshowGenerator
{
    /// <summary>
    /// Contains classes which contain constants for configuring the application and member, which are important for the workflow.
    /// </summary>
    public static class Configuration
    {
        private static readonly string _configFile = String.Format("{0}\\{1}", Application.StartupPath, "configuration.isc");
        private static DataTable _dt_config = new DataTable("Configuration");
        private static DataColumn _dc_name = new DataColumn("Name", typeof(string));
        private static DataColumn _dc_value = new DataColumn("Value", typeof(string));

        /// <summary>
        /// Contains constants which can be used, throughout the application.
        /// </summary>
        public static class Constants
        {
            /// <summary>
            /// Width of Image Preview.
            /// </summary>
            public const int ImagePreviewWidth = 568;
            
            /// <summary>
            /// Height of Image Preview.
            /// </summary>
            public const int ImagePreviewHeight = 320;
        }

        public static class Languages
        {
            public const string English = "English";
            public const string Turkish = "Türkçe";
        }

        /// <summary>
        /// Contains the names of the possible configurations.
        /// </summary>
        public static class Names
        {
            public const string CAPTION_IMAGE_FREQUENCY = "CaptionImageFrequency";
            public const string IMAGE_WIDTH = "ImageWidth";
            public const string IMAGE_HEIGHT = "ImageHeight";
            public const string USE_ORIGINAL_SIZES = "UseOriginalSizes";
            public const string LANGUAGE = "Language";
        }

        /// <summary>
        /// Contains the default values of configurations.
        /// </summary>
        public static class DefaultValues
        {
            public const int CAPTION_IMAGE_FREQUENCY = 3;
            public const int IMAGE_WIDTH = 1280;
            public const int IMAGE_HEIGHT = 720;
            public const bool USE_ORIGINAL_SIZES = true;
        }

        static Configuration()
        {
            _dt_config.Columns.Add(_dc_name);
            _dt_config.Columns.Add(_dc_value);
            LoadConfiguration();
        }

        private static int _captionImageFrequency = 3;

        /// <summary>
        /// Frequency in which the caption image will show up.
        /// </summary>
        public static int CaptionImageFrequency
        {
            get { return Configuration._captionImageFrequency; }
            set
            {
                Configuration._captionImageFrequency = value;
                _dt_config.Select(String.Format("{0} = '{1}'", _dc_name.ColumnName, 
                    Names.CAPTION_IMAGE_FREQUENCY))[0].SetField<string>(_dc_value, value.ToString());
            }
        }

        private static int _imageWidth = 1280;

        /// <summary>
        /// Maximum width of images.
        /// </summary>
        public static int ImageWidth
        {
            get { return Configuration._imageWidth; }
            set
            {
                Configuration._imageWidth = value;
                _dt_config.Select(String.Format("{0} = '{1}'", _dc_name.ColumnName,
                    Names.IMAGE_WIDTH))[0].SetField<string>(_dc_value, value.ToString());
            }
        }

        private static int _imageHeight = 720;

        /// <summary>
        /// Maximum height of images.
        /// </summary>
        public static int ImageHeight
        {
            get { return Configuration._imageHeight; }
            set
            {
                Configuration._imageHeight = value;
                _dt_config.Select(String.Format("{0} = '{1}'", _dc_name.ColumnName,
                    Names.IMAGE_HEIGHT))[0].SetField<string>(_dc_value, value.ToString());
            }
        }

        private static bool _useOriginalSizes = true;

        /// <summary>
        /// Indicates whether the original sizes of the images should remain in the slideshow.
        /// If true, the properties ImageWidth and ImageHeight are innecessarry.
        /// </summary>
        public static bool UseOriginalSizes
        {
            get { return Configuration._useOriginalSizes; }
            set
            {
                Configuration._useOriginalSizes = value;
                _dt_config.Select(String.Format("{0} = '{1}'", _dc_name.ColumnName,
                    Names.USE_ORIGINAL_SIZES))[0].SetField<string>(_dc_value, value.ToString());
            }
        }

        private static string _language = Configuration.Languages.English;

        public static string Language
        {
            get { return Configuration._language; }
            set
            {
                Configuration._language = value;
                _dt_config.Select(String.Format("{0} = '{1}'", _dc_name.ColumnName,
                    Names.LANGUAGE))[0].SetField<string>(_dc_value, value.ToString());
            }
        }

        /// <summary>
        /// Loads Configuration to the intern variables.
        /// </summary>
        public static void LoadConfiguration()
        {
            if (File.Exists(_configFile))
            {
                _dt_config.Clear();
                _dt_config.ReadXml(_configFile);
                foreach (DataRow dr in _dt_config.Rows)
                {
                    switch (dr.Field<string>(_dc_name))
                    {
                        case Configuration.Names.CAPTION_IMAGE_FREQUENCY:
                            _captionImageFrequency = Convert.ToInt32(dr.Field<string>(_dc_value));
                            break;
                        case Configuration.Names.IMAGE_WIDTH:
                            _imageWidth = Convert.ToInt32(dr.Field<string>(_dc_value));
                            break;
                        case Configuration.Names.IMAGE_HEIGHT:
                            _imageHeight = Convert.ToInt32(dr.Field<string>(_dc_value));
                            break;
                        case Configuration.Names.USE_ORIGINAL_SIZES:
                            _useOriginalSizes = Convert.ToBoolean(dr.Field<string>(_dc_value));
                            break;
                        case Configuration.Names.LANGUAGE:
                            _language = dr.Field<string>(_dc_value);
                            break;
                    }
                }
            }
            else //if config-file does not exist, create the configs and save file
            {
                _dt_config.Rows.Add(Configuration.Names.CAPTION_IMAGE_FREQUENCY, _captionImageFrequency);
                _dt_config.Rows.Add(Configuration.Names.IMAGE_WIDTH, _imageWidth);
                _dt_config.Rows.Add(Configuration.Names.IMAGE_HEIGHT, _imageHeight);
                _dt_config.Rows.Add(Configuration.Names.USE_ORIGINAL_SIZES, _useOriginalSizes);
                _dt_config.Rows.Add(Configuration.Names.LANGUAGE, _language);

                SaveConfiguration();
            }
        }

        /// <summary>
        /// Saves Configuration to the XML-File in the application folder.
        /// </summary>
        public static void SaveConfiguration()
        {
            _dt_config.WriteXml(_configFile);
        }
    }
}
